package com.lmxdawn.api.im.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.lmxdawn.api.im.service.ImUtil;
import io.lettuce.core.RedisClient;
import lombok.var;
import org.jim.core.ImChannelContext;
import org.jim.core.exception.ImException;
import org.jim.core.packets.User;
import org.jim.server.JimServerAPI;
import org.jim.server.listener.AbstractImUserListener;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author WChao
 * @Desc
 * @date 2020-05-02 18:18
 */
public class ImDemoUserListener extends AbstractImUserListener {

    private static Logger logger = LoggerFactory.getLogger(ImDemoUserListener.class);

    @Override
    public void doAfterBind(ImChannelContext imChannelContext, User user) throws ImException {
        logger.info("绑定用户:{}", JSONObject.toJSONString(user));// 用户 + 1
        if (user.getStatus().equals("online")
                && !"0".equals(user.getUserId())
                && !"客服".equals(user.getUserId())
                && !"admin".equals(user.getUserId())){
            ImUtil.moveUserToFirst(user.getUserId());
        }
    }

    @Override
    public void doAfterUnbind(ImChannelContext imChannelContext, User user) throws ImException {
        logger.info("解绑用户:{}",JSONObject.toJSONString(user)); // 用户 -1
        if (user.getStatus().equals("offline")
                && !"0".equals(user.getUserId())
                && !"客服".equals(user.getUserId())
                && !"admin".equals(user.getUserId())){
            ImUtil.moveUser(user.getUserId(),-1);
        }
    }
}